package com.example.cadmin.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.cadmin.entity.MyDept;
import com.example.cadmin.entityVo.MenuALLVo;
import com.example.cadmin.mapper.MyDeptMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.cadmin.utils.DateFormat;
import com.example.cadmin.utils.ResponseModel;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author zzsoft
 * @since 2023-02-18
 */
@Service
public class MyDeptService extends ServiceImpl<MyDeptMapper, MyDept> {

    @Resource
    private MyDeptMapper myDeptMapper;

    /**
     * 部门表格
     */
    public Map<String, Object> deptList(String deptName, int page, int limit) {
        IPage<Map<String,Object>> deptPage = new Page<>(page, limit);//参数一是当前页，参数二是每页个数
        QueryWrapper<MyDept> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(deptName)) {
            wrapper.like("dept_name", deptName);
        }
        wrapper.orderByAsc("sort");
        deptPage = myDeptMapper.selectMapsPage(deptPage,wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "数据请求成功");
        map.put("count", deptPage.getTotal());
        map.put("data", deptPage.getRecords());
        return map;
    }

    /**
     * 绘制部门树
     */
    public ResponseModel buildDeptAll() {
        List<MenuALLVo> sysUserVoList = myDeptMapper.buildDeptAll();
        return ResponseModel.success(sysUserVoList);
    }


    /**
     * 新增部门保存
     */
    public ResponseModel addDept(JSONObject data) {
        QueryWrapper<MyDept> deptQueryWrapper = new QueryWrapper<MyDept>();
        deptQueryWrapper.eq("dept_name", data.getStr("deptName"));
        MyDept myDept = myDeptMapper.selectOne(deptQueryWrapper);
        if (myDept != null) {
            return ResponseModel.fail("部门名称重复");
        }
        MyDept insertMyDept = new MyDept();
        if (StrUtil.isEmpty(data.getStr("parentId"))) {
            insertMyDept.setParentId(0);
        } else {
            insertMyDept.setParentId(data.getInt("parentId"));
        }
        insertMyDept.setDeptName(data.getStr("deptName"));
        insertMyDept.setSort(data.getInt("sort"));
        insertMyDept.setStatus(data.getInt("status"));
        insertMyDept.setCreateTime(DateFormat.getTime());
        insertMyDept.setUpdateTime(DateFormat.getTime());
        int insertStatus = myDeptMapper.insert(insertMyDept);
        return insertStatus > 0 ? ResponseModel.success("新增  " + data.getStr("deptName") + "  部门成功") : ResponseModel.fail("新增部门失败");
    }

    /**
     * 部门停用和启用
     */
    @ResponseBody
    public ResponseModel updateStatus(JSONObject data) {
        int updateStatus = myDeptMapper.update(null, new UpdateWrapper<MyDept>().eq("dept_id", data.getInt("deptId"))
                .set("status", data.getInt("status")));
        if (updateStatus > 0) {
            if (data.getInt("status") == 0) {
                return ResponseModel.success("部门已禁用");
            } else {
                return ResponseModel.success("部门已启用");
            }
        } else {
            return ResponseModel.fail("修改部门状态失败");
        }
    }

    /**
     * 修改部门信息
     */
    public ResponseModel editDept(JSONObject data) {
        UpdateWrapper<MyDept> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("dept_id", data.getInt("deptId")).set("parent_id", data.getInt("parentId"))
                .set("dept_name", data.getStr("deptName")).set("sort", data.getInt("sort")).set("status", data.getInt("status"));
        int updateStatus = myDeptMapper.update(null, updateWrapper);
        return updateStatus > 0 ? ResponseModel.success("修改部门信息成功") : ResponseModel.fail("修改部门信息失败");
    }

    /**
     * 删除部门
     */
    public ResponseModel deleteDept(String deptId){
        String[] strings = deptId.split(",");
        List<String> idList = CollUtil.newArrayList(strings);
        int delCount = myDeptMapper.deleteBatchIds(idList);
        return delCount > 0 ? ResponseModel.success("删除部门信息成功") : ResponseModel.fail("删除部门信息失败");
    }
}
